 <template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      error-text="加载失败，请点击重试"
      :error="error"
      @load="onLoad"
    >
      <van-cell v-for="(item,index) in list" :key="index" :title="item.title" />
    </van-list>
  </div>
</template>
   
   <script>
   import {getSearchResult} from '@/api/search'
export default {
  name: "searchResult",
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page:1,
      perPage:20,
      error:false

    };
  },
  methods: {
   async onLoad() {
      // 1.请求获取数据
      try{
         const {data} = await getSearchResult({
           page:this.page, //页码
           per_page: this.perPage, //每页大小
           q: this.searchText //查询关键词  
         })
         console.log(data);
          // 2.将数据添加到数组列表
          const {results} = data.data
          this.list.push(...results)
          // console.log(list);
          // 3.将本次加载中的 loading 关闭
         this.loading = false
        // 4.判断是否还有数据
            
           
            if(results.length){
            // 如果有，则更新获取下一个数据页码
             this.page++
            } else{
             // 如果没有，则将加载状态 finished 设置为结束
             this.finished = true
            }
      } catch(err){
        //展示加载失败的提示状态
        this.error = true
        //加载失败了 loading也要关闭
        this.loading = false
      }
    },
  },
  props:{
    searchText:{
      type:String,
      require:true
    }
  }
};
</script>
   
   <style lang="less" scoped>
</style>
   